package es.shinra.app.properties;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * a.
 * @author Huchnet
 */
public class Config implements iConfig {
    private List taxonomyList;
    private Properties defaultProps, dbquery;
    private Properties customProps;
    private InputStream indef, in, inquery;
    private OutputStream outdef;
    private FileOutputStream out;
    private Properties taxonomyProps;
    //OutputStream outdef, out;

    /**
     * a.
     * @throws java.io.FileNotFoundException a.
     */
    public Config() throws FileNotFoundException {
        indef = Config.class.getClassLoader().getResourceAsStream("default.properties");
        out = new FileOutputStream("src/main/resources/default.properties");
        inquery = Config.class.getClassLoader().getResourceAsStream("query.properties");
        taxonomyProps=new Properties();
        defaultProps = new Properties();
        dbquery = new Properties();
        in = Config.class.getClassLoader().getResourceAsStream("custom.properties");
    }

    /**
     * a.
     */
    public void init() {
        try {
            taxonomyProps.load(Config.class.getClassLoader().getResourceAsStream("taxonomy.properties"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            defaultProps.load(indef);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        customProps = new Properties();
        try {
            customProps.load(in);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
        try {
            indef.close();
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        defaultProps.setProperty("driver", "org.exist.xmldb.DatabaseImpl");
        defaultProps.setProperty("Uri", "xmldb:exist://localhost:8080/exist/xmlrpc/db/dbApp/");
        defaultProps.setProperty("imageURL", "/Users/Huchnet/Desktop/Images/");
        defaultProps.setProperty("programURL", "/Users/Huchnet/NetBeansProjects/tortuns/trunk/PhotoShared");
        defaultProps.setProperty("gmailAccount", "huchnet@gmail.com");
        defaultProps.setProperty("gmailPassword", "");
        
        try {
            defaultProps.store(out, "");
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            dbquery.load(inquery);
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * a.
     * @param aName a.
     * @return a.
     */
    public String getQuery(String aName) {
        return dbquery.getProperty(aName);
    }

    /**
     * a.
     * @return a.
     */
    public String getUri() {
        if (customProps.getProperty("Uri") != null) {
            return customProps.getProperty("Uri");

        }
        return defaultProps.getProperty("Uri");
    }


    /**
     * a.
     * @return a.
     */
    public String getDriver() {
        if (customProps.getProperty("driver") != null) {
            return customProps.getProperty("driver");
        }
        return defaultProps.getProperty("driver");
    }

    /**
     * a.
     * @return a.
     */
    public String getUrlPath() {
        if (customProps.getProperty("imageURL") != null) {
            
            return customProps.getProperty("imageURL");
        }
        return defaultProps.getProperty("imageURL");
    }

    /**
     * a.
     * @param property a.
     * @return a.
     */
    public String getProperty(String property) {
        if (customProps.getProperty(property) != null) {
            return customProps.getProperty(property);
        }
        return defaultProps.getProperty(property);
    }

    /**
     * a.
     * @param Uri
     */
    public void setUri(String Uri) {

        customProps.setProperty("Uri", Uri);
        try {
            customProps.store(new FileOutputStream("src/main/resources/custom.properties"), "");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * a.
     * @param driver a.
     */
    public void setDriver(String driver) {
        customProps.setProperty("driver", driver);
        try {
            customProps.store(new FileOutputStream("src/main/resources/custom.properties"), "");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    /**
     * a.
     * @param property a.
     * @param value a.
     */
    public void setProperty(String property, String value) {
        customProps.setProperty(property, value);
        try {
            customProps.store(new FileOutputStream("src/main/resources/custom.properties"), "");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * a.
     */
    public void close() {
    
    }

    public String getTaxonomy(String aTaxonomy) {
        return taxonomyProps.getProperty(aTaxonomy);
    }

    public void setTaxonomy(String file, String aTaxonomy) {
        taxonomyProps.setProperty(file, aTaxonomy);
        try {
            taxonomyProps.store(new FileOutputStream("src/main/resources/taxonomy.properties"), "");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public Enumeration getTaxonomyList() {
        return taxonomyProps.propertyNames();
    }
}